package com.PlantVsZombies.game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.PlantVsZombies.constant.Constant;
import com.PlantVsZombies.constant.GameVariable;
import com.PlantVsZombies.db.dao.PassDao;
import com.PlantVsZombies.db.model.Pass;
import com.PlantVsZombies.pass.PassBuilder;
import com.PlantVsZombies.ui.GamePanel;

public class PassEditPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private ButtonGroup group;
	private JRadioButton jRadioButton0;
	private JRadioButton jRadioButton1;
	private JComboBox jComboBox0;
	private JButton jButton0;
	private JButton jButton1;
	private JFrame frame = null;
	private PassDao passDao = new PassDao();

	public PassEditPanel(JFrame frame) {
		this.frame = frame;
		initComponents();
	}

	private void initComponents() {
		setLayout(null);
		add(getJRadioButton0());
		add(getJRadioButton1());
		add(getJButton0());
		add(getJComboBox0());
		add(getJButton1());
		initGroup();
		setSize(397, 134);
	}

	private void initGroup() {
		group = new ButtonGroup();
		group.add(getJRadioButton0());
		group.add(getJRadioButton1());
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("返回");
			jButton1.setBounds(263, 77, 81, 28);
			jButton1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("确定");
			jButton0.setBounds(27, 78, 89, 28);
			jButton0.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (jRadioButton0.isSelected()){
						
					} else {
						List<Pass> passList = passDao.selectByPlayer(GameVariable.player);
						
						String name = (String) jComboBox0.getSelectedItem();
						for (Pass pass : passList) {
							if (pass.getName().equals(name)) {
								GameVariable.temp_list = PassBuilder.jsonToPass(pass.getData());
								GameVariable.temp_pass_name = name;
								break;
							}
						}
					}
					new PassCustomFrame();
					
					frame.dispose();
				}
			});
		}
		return jButton0;
	}

	private JComboBox getJComboBox0() {
		if (jComboBox0 == null) {
			jComboBox0 = new JComboBox();
			jComboBox0.setDoubleBuffered(false);
			jComboBox0.setBorder(null);
			jComboBox0.setBounds(257, 34, 89, 27);
		}
		return jComboBox0;
	}

	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setSelected(true);
			jRadioButton1.setText("修改关卡");
			jRadioButton1.setBounds(149, 35, 104, 26);
			jRadioButton1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					List<Pass> passList = passDao.selectByPlayer(GameVariable.player);
					if (passList.size() == 0) {
						// 还未添加 提示添加
						//定制可供选择按钮
						Object[] options = {"确定","取消"};
						int response=JOptionPane.showOptionDialog(null, "是否添加自定义关卡?","温馨提示",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
						// 确定
						if(response==0) {         
							new PassCustomFrame();
							frame.dispose();
						} else if(response==1) {  
							// 取消
							jRadioButton0.setSelected(true);
							jRadioButton1.setSelected(false);
							return;
						}
					}
					String [] passName = new String[passList.size()];
					for (int i=0; i< passList.size(); i++) {
						passName[i] = passList.get(i).getName();
					}
					jComboBox0.setModel(new DefaultComboBoxModel<String>(passName));
				}
			});
		}
		return jRadioButton1;
	}

	private JRadioButton getJRadioButton0() {
		if (jRadioButton0 == null) {
			jRadioButton0 = new JRadioButton();
			jRadioButton0.setSelected(true);
			jRadioButton0.setText("添加关卡");
			jRadioButton0.setBounds(37, 35, 104, 26);
		}
		return jRadioButton0;
	}

}
